package demo.messaging.server;

import java.util.ArrayList;
import java.util.List;

import demo.messaging.common.AbstractMessage;
import demo.messaging.common.EnumUserStatus;

public final class UserContext {

	private final String userName;

	private EnumUserStatus status = EnumUserStatus.free;

	private final List<AbstractMessage> receivedMessages = new ArrayList<AbstractMessage>();

	/**
	 * Last time the server interacts with the client.
	 */
	long lastInteractionTime = System.currentTimeMillis();

	private final String sessionId;

	public UserContext(final String userName, final String sessionId) {
		this.userName = userName;
		this.sessionId = sessionId;
	}

	public synchronized void addMessage(final AbstractMessage message) {
		this.receivedMessages.add(message);
	}

	public long getLastInteractionTime() {
		return this.lastInteractionTime;
	}

	public String getSessionId() {
		return this.sessionId;
	}

	public EnumUserStatus getStatus() {
		return this.status;
	}

	public String getUserName() {
		return this.userName;
	}

	public synchronized List<? extends AbstractMessage> retrieveMessages() {
		List<? extends AbstractMessage> result = new ArrayList<AbstractMessage>(
				this.receivedMessages);
		this.receivedMessages.clear();
		return result;
	}

	public void setStatus(final EnumUserStatus status) {
		this.status = status;
	}

	public void updateLastInteractionTime() {
		this.lastInteractionTime = System.currentTimeMillis();
	}

}
